<?php

/**
 * 工具管理
 * @author Administrator
 */

namespace Operation\Controller;

use Operation\Model\Goods;
use Think\Controller;
use Think\IReq;
use Think\IFilter;
use Think\JSON;
use Think\IUrl;
class ToolController extends ComController {

    //备份数据库
    public function db_bak() {
        $db = M();
        $tableInfo = $db->query($sql = 'show table status');

        foreach ($tableInfo as $key => $value) {
            if($value['data_length'] > 1024){
                $tableInfo[$key]['data_length'] = ($value['data_length'] >> 10) . 'KB';
            }  else {
                $tableInfo[$key]['data_length'] = ($value['data_length']) . 'KB';
            }
        }
        //dump($tableInfo);die;
        $this->assign('tableInfo', $tableInfo);
        $this->display();
    }

    //[备份还原]数据备份动作(ajax操作)
    function db_act_bak() {
        //要备份的数据表
        $tableName = IReq::get('name', 'post');
        $tableName = IFilter::act($tableName, "string");

        //分卷大小限制(KB)
        $partSize = 4000;

        if (is_array($tableName) && isset($tableName[0]) && $tableName[0] != '') {
            $backupObj = new DBBackup($tableName);
            $backupObj->setPartSize($partSize);   //设置分卷大小
            $backupObj->runBak();                 //开始执行
            $result = array(
                'isError' => false,
                'redirect' => IUrl::creatUrl('/tools/db_res'),
            );
        } else {
            $result = array(
                'isError' => true,
                'message' => '请选择要备份的数据表',
            );
        }
        echo JSON::encode($result);
    }

}